package app.fugema.shop.main.order.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.base.common.app.LoginUtils;
import com.base.common.model.http.jackSon.JacksonUtils;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.DateUtils;
import com.base.common.utils.OnClickCheckedUtil;
import com.base.common.utils.UIUtils;
import com.base.common.view.base.BaseTitleActivity;
import com.base.common.viewmodel.BaseViewObserver;

import java.util.List;

import app.fugema.shop.R;
import app.fugema.shop.databinding.ActivityRefundOrderDetailBinding;
import app.fugema.shop.main.evaluate.view.EvaluateAddActivity;
import app.fugema.shop.main.order.adapter.RefundGoodsAdapter;
import app.fugema.shop.main.order.model.RefundDetailResult;
import app.fugema.shop.main.order.model.RefundOrderItem;
import app.fugema.shop.main.order.model.RefundProcessDetailResult;
import app.fugema.shop.main.order.vm.OrderAfterSaleVM;

public class OrderAfterSaleDetailActivity extends BaseTitleActivity<ActivityRefundOrderDetailBinding, OrderAfterSaleVM>
{

    public static void startByRefundId(Context context, int refundId ,String productList )
    {
        Intent intent = new Intent(context, OrderAfterSaleDetailActivity.class);
        intent.putExtra("refundId", refundId);
        //,String shopName ,String productList
        //intent.putExtra("shop", shopName);
        intent.putExtra("productlist", productList);
        context.startActivity(intent);
    }

    public static void start(Context context, int refundId ,String shopName ,String productList)
    {
        Intent intent = new Intent(context, OrderAfterSaleDetailActivity.class);
        intent.putExtra("refundId", refundId);
        intent.putExtra("shop", shopName);
        intent.putExtra("productlist", productList);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_refund_order_detail);
    }

    private RefundDetailResult refundDetail;

    private int refundId;
    private String userKey;

    private String mShopName ="";
    private List<RefundOrderItem> refundList;
    private RefundGoodsAdapter adapter;
    @Override
    protected int getStatusBarColor()
    {
        return R.color.redColor;
    }
    @Override
    protected void initView()
    {
        super.initView();

        setCloseImg(R.mipmap.ic_back_white);
        setTitle("售后详情",R.color.white);

        /*
        binding.tvShopNameTitle.setOnClickListener(v -> {
            //退款流程 备注详情
            viewModel.refundProcessDetail( refundId,userKey )
                     .observe(this, new BaseViewObserver<LiveDataWrapper<RefundProcessDetailResult>>() {
                          @Override
                          protected void onSuccess(LiveDataWrapper<RefundProcessDetailResult> data)
                          { }
                      });
        });
        */
    }


    @Override
    protected void initData(Intent intent)
    {
        super.initData(intent);
        refundId = intent.getIntExtra("refundId",0);
        mShopName = intent.getStringExtra("shop");
        String goodsListStr = intent.getStringExtra("productlist");
        if( UIUtils.isNotEmpty( goodsListStr ) )
        {
            refundList = JacksonUtils.getConverList( goodsListStr,RefundOrderItem.class );
        }

        userKey = LoginUtils.getToken();
        if( UIUtils.isNotEmpty( mShopName ) )
        {
            binding.tvShopNameTitle.setText( mShopName );
        }
        else
        {
            binding.givStoreTag.setVisibility(View.GONE);
            binding.tvShopNameTitle.setVisibility(View.GONE);
        }

        adapter = new RefundGoodsAdapter();
        binding.rvRecyclerViewGoods.setAdapter(adapter);
        if( UIUtils.isNotEmpty( adapter ) )
        {
          adapter.setDataList(refundList);
        }
        viewModel.refundOrderDetail(refundId,userKey)
                 .observe(this, new BaseViewObserver<LiveDataWrapper<RefundDetailResult>>(this)
                 {
                    @Override
                    protected void onSuccess(LiveDataWrapper<RefundDetailResult> data) {
                        refundDetail = data.data ;
                        binding.setRefund(refundDetail);
                        binding.executePendingBindings();

                        //1=待商家审核,2=待买家寄货,3=待商家收货,4=商家拒绝,5=商家通过审核,6=待平台确认,7=退款成功
                        if( refundDetail.getSellerAuditStatusValue() == 4 )
                        {
                            binding.ivStatusImg.setImageResource(R.mipmap.ic_order_refuse);

                            if( UIUtils.isNotEmpty( refundDetail.getSellerRemark() ) )
                            {
                                binding.rlRefuseReasonLayout.setVisibility(View.VISIBLE);
                                binding.tvRefuseReason.setText( refundDetail.getSellerRemark() );
                            }
                        }

                        if(refundDetail.getManagerConfirmStatusValue() == 7 )
                        {
                            binding.ivStatusImg.setImageResource(R.mipmap.ic_order_complate);
                        }
                    }

                    @Override
                    public boolean isEmptyData(LiveDataWrapper<RefundDetailResult> data)
                    {
                        return UIUtils.isEmpty(data.data.getRefundStatus());
                    }
        });
    }

    private void showLastTime()
    {
        long time = DateUtils.dateStringToLong("getEndHandleTime()");//	截止处理时间	string(date-time)
        long spe = time - System.currentTimeMillis();
        if (spe > 0) {
            long m = spe / (60 * 60 * 1000);//多少小时
            long mText = m % 24;
            long hText = m / 24;
            //binding.tvTime.setText("还剩" + hText + "天" + mText + "小时自动确认");
        }
    }
}
